package org.ihelpuoo.orm.handler.dom;

import java.lang.reflect.Field;

import javax.xml.xpath.XPathConstants;

import org.ihelpuoo.orm.ObjectRule;
import org.ihelpuoo.orm.handler.ObjectProcessor;
import org.ihelpuoo.orm.handler.ReadObjectFieldHandler;
import org.w3c.dom.Node;

public class EleReadObjectFieldHandler extends ReadObjectFieldHandler
{

	public EleReadObjectFieldHandler(ObjectProcessor beanHandler)
	{
		super(beanHandler);
	}

	@Override
	protected Object evalSubObject(ObjectRule pr, Field f, Object src, Object subObj)
			throws Exception
	{
		String tagName = pr.getDomNodeName(f);

		Node child = (Node)this.processor.getManual().xpath().newXPath().evaluate(
				"./" + tagName + "[1]",src,XPathConstants.NODE);
		if (null != child)
		{
			ObjectRule subRule = processor.getManual().getRule(subObj.getClass());
			processor.fetch(subRule,child,subObj,null,null, null);
			return subObj;
		}
		return null;
	}

}
